﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.Web.RealtyManagers
{
    public partial class HouseholdersManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindHouseholders();
            }
        }

        private void BindHouseholders()
        {
            using (var dataContext = new realtydbEntities())
            {
                var users = (from u in dataContext.Households.Include("aspnet_Users.aspnet_Membership").Include("Building")
                             select new
                             {
                                 Id = u.HouseholdID,
                                 BuNo = u.Building.BuildingName,
                                 UnNo = u.UnitNo,
                                 DepNo = u.DepartmentNo,
                                 Name = u.aspnet_Users.UserName,
                                 Corp = u.Corporation,
                                 IdNumber = u.IdNumber,
                                 Telephone = u.Telephone,
                                 OtherHouseMembers = u.OtherHouseMembers,
                                 OtherInfo = u.OtherInfo,
                                 CrDate = u.aspnet_Users.aspnet_Membership.CreateDate
                             });
                this.gvHouse.DataSource = users;
                this.gvHouse.DataBind();
            }
        }

        protected void gvHouse_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            dynamic hou = gvHouse.Rows[e.RowIndex].DataItem;
            Guid HouseHoldid = Guid.Parse(hou.Id);

            using (var dataContext = new realtydbEntities())
            {
                var house = (from h in dataContext.Households.Include("aspnet_Users.aspnet_Membership")
                             where h.HouseholdID == HouseHoldid
                             select h).First();
                dataContext.DeleteObject(house);
                dataContext.SaveChanges();
            }
            BindHouseholders();
        }
    }//class
}